/**	
 * <br>
 * Copyright 2011 IFlyTek. All rights reserved.<br>
 * <br>			 
 * Package: com.iflytek.gwrq.base <br>
 * FileName: AbstractService.java <br>
 * <br>
 * @version
 * @author sbwang@iflytek.com
 * @created 2013-5-3
 * @last Modified 
 * @history
 */

package com.iflytek.ci.base;

import com.iflytek.iframework.orm.hibernate.HibernateEntityDao;
import com.iflytek.iframework.utils.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * {该处请说明该class的含义和作用}
 *  
 *  @author sbwang@iflytek.com
 *  @lastModified       
 *  @history           
 */

public abstract class AbstractService<T> extends HibernateEntityDao<T> {

    /**
     * 日志
     */
    protected final Log logger = LogFactory.getLog(this.getClass());
    
    /**
     * 
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getSystemDateTimeGabString()
     *  @author sbwang@iflytek.com
     *  @created 2013-5-14 下午10:30:57
     *  @lastModified      
     *  @history
     */
    public String getSystemDateTimeGabString() {
        return (String) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = getSession()
                                .createSQLQuery(
                                        " SELECT to_char(sysdate,'yyyymmddhh24miss') FROM DUAL");
                        String str = (String) query.uniqueResult();
                        return str;
                    }
                });
    }
    
    
    /**
     * 
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getSystemDateTimeString()
     *  @author scrum
     *  @created 2013-6-18 下午10:23:04
     *  @lastModified      
     *  @history
     */
    public String getSystemDateTimeString() {
        return (String) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = getSession()
                                .createSQLQuery(
                                        " SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') FROM DUAL");
                        String str = (String) query.uniqueResult();
                        return str;
                    }
                });
    }
    
    /**
     * 
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getSystemDateTime()
     *  @author scrum
     *  @created 2013-6-18 下午10:22:35
     *  @lastModified      
     *  @history
     */
    public Date getSystemDateTime() {
        return (Date) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = getSession()
                                .createSQLQuery(
                                        " SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') FROM DUAL");
                        String str = (String) query.uniqueResult();
                        return DateUtils.toDate(str, "yyyy-MM-dd HH:mm:ss");
                    }
                });
    }
    
    
    /**
     * 
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getCount(String)
     *  @author scrum
     *  @created 2013-6-18 下午10:23:45
     *  @lastModified
     *  @history
     */
    public Long getCount(final String queryString) {
        return (Long) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = getSession().createQuery(queryString);
                        return query.uniqueResult();
                    }
                });
    }

    /**
     *
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getCount(String, Object[])
     *  @author scrum
     *  @created 2013-6-18 下午10:24:09
     *  @lastModified
     *  @history
     */
    public Long getCount(final String queryString, final Object... values) {
        return (Long) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = session.createQuery(queryString);
                        int i = 0;
                        for (Object o : values) {
                            query.setParameter(i, o);
                            i++;
                        }
                        return query.uniqueResult();
                    }
                });
    }

    /**
     *
     *  @see com.iflytek.iframework.orm.hibernate.HibernateGenericDao#getTopQuery(String, int, int, Object[])
     *  @author scrum
     *  @created 2013-6-18 下午10:24:45
     *  @lastModified      
     *  @history
     */
    @SuppressWarnings("unchecked")
    public List<T> getTopQuery(String querySql, int sp, int size,
            final Object... values) {
        Query query = getSession().createQuery(querySql);
        query.setFirstResult(sp);
        query.setMaxResults(size);
        int i = 0;
        for (Object o : values) {
            query.setParameter(i, o);
            i++;
        }
        return query.list();
    }
    
    
    /**
     * 
     *  获取数据库系统时间
     *  @return
     *  @author scrum
     *  @created 2013-6-12 下午10:42:19
     *  @lastModified       
     *  @history
     */
    public String getJartDay(){
        return (String) this.getHibernateTemplate().execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session)
                            throws HibernateException, SQLException {
                        Query query = getSession()
                                .createSQLQuery(
                                        " SELECT to_char(sysdate,'yyyy-mm-dd') FROM DUAL");
                        String str = (String) query.uniqueResult();
                        return str;
                    }
                });
    }
    
    /**
     * 
     *  格式化日期格式将14为日期时间字符串格式化问 YYYY-MM-DD HH:MM:SS的时间格式
     *  @param hdfssj
     *  @return
     *  @author yali
     *  @created 2013-7-3 上午09:04:17
     *  @lastModified       
     *  @history
     */
    public String parseDateFormat(String hdfssj){
        if (hdfssj != null && hdfssj.length() == 14) {
            String date = hdfssj.substring(0, 4) + "-" + hdfssj.substring(4, 6)
                    + "-" + hdfssj.substring(6, 8) + " " + hdfssj.substring(8, 10)
                    + ":" + hdfssj.substring(10, 12)+":"+hdfssj.substring(12,14);
            return date;
        } else {
            return "";
        }
    }
    
}




